const Model = require('../model/multil')
const mongoose = require('mongoose')
const { System } = Model
const { Brand } = Model
const { Phone } = Model
const systemController = {
    async createSystem(req, res) {
         const body = req.query
         try {
             await System.findOne({systemName:body.systemName}).exec((err,resulit)=> {
                if(resulit) {
                    return res.status(201).json({
                        err_code: 1,
                        message: 'Email or nick name already exists!'
                    })
                 }  
                 new System(body).save()
                 res.status(200).json({
                    err_code: 0,
                    message: '添加用户成功！'  
                })
             })
         } catch (error) {
            res.status(500).json({
                error_code: 1,
                message: '服务器错误'
            })
         }

    },
    async createBrand(req, res) {
        const body = req.query
        try {
            await Brand.findOne({brandName:body.brandName}).exec((err,resulit)=> {
               if(resulit) {
                   return res.status(201).json({
                       err_code: 1,
                       message: 'Email or nick name already exists!'
                   })
                }  
                new Brand(body).save()
                res.status(200).json({
                   err_code: 0,
                   message: '添加用户成功！'  
               })
            })
        } catch (error) {
           res.status(500).json({
               error_code: 1,
               message: '服务器错误'
           })
        }

    },
    async createPhone(req, res) {
        const body = req.query
        try {
            await Phone.findOne({name:body.name}).exec((err,resulit)=> {
            //    if(resulit) {
            //        return res.status(201).json({
            //            err_code: 1,
            //            message: 'Email or nick name already exists!'
            //        })
            //     }  
                new Phone(body).save()
                res.status(200).json({
                   err_code: 0,
                   message: '添加用户成功！'  
               })
            })
        } catch (error) {
           res.status(500).json({
               error_code: 1,
               message: '服务器错误'
           })
        }

    },
    async findPhone(req,res) {
        try {
          await System.find().populate({
            path: 'brands',
            select: 'brandName',
            model: 'Brand',
            populate: {
                path: 'phones',
                select: ' name price',
                model: 'Phone'
            }
        }).exec((err,result)=>{
            console.log(System)
            return res.status(200).json(result)
          })
        } catch (error) {
          res.status(500).json({
            error_code: 1,
            message: '服务器错误'
          })
        }
    },
    async createMulit(req,res) {
        const body = req.query
        let Phone1 = new Phone({
        _id: new mongoose.Types.ObjectId(),
        name: body.name,
        price: body.price
      })
      try {
        await Phone.findOne({}).exec((err,doc)=>{
             Phone1.save()
             let Brand1 = new Brand({
                _id: new mongoose.Types.ObjectId(),
                brandName: body.brandName,
                phones:Phone1._id
              })
              Brand1.save()
              let System1 = new System({
                _id: new mongoose.Types.ObjectId(),
                systemName: body.systemName,
                brands:Brand1._id
              })
              System1.save()
              res.status(200).json(doc)
        })
      } catch (error) {
       res.status(500).json({
         error_code: 1,
         message: '服务器错误'
       })
      }
    },
    async deleteTest(req,res) {
        const body = req.query
        let _id = body.name
        try {
            await Phone.findOne({_id:_id}).remove().exec((err,doc)=>{
                    res.status(200).json(doc)
            })

          } catch (error) {
           res.status(500).json({
             error_code: 1,
             message: '服务器错误'
           })
          }
    },
    async createSpecial(req,res) {
        const body = req.query
        var Phone1 = new Phone({
            _id: new mongoose.Types.ObjectId(),
            name: body.name,
            price: body.price
        })
        try {
            await Phone.findOne({}).exec((err,doc)=>{

                 Phone1.save()  
                 Brand.updateOne({'_id':"55e6063732a8d5806aca55eea"},{$push:{phones:Phone1._id}}).exec((err,doc)=>{
                    res.status(200).json(doc)
                })
            })

          } catch (error) {
           res.status(500).json({
             error_code: 1,
             message: '服务器错误'
           })
          }
    }
    // async createSpecial(req,res) {
    //     const body = req.query
    //     var brand1 = new Brand({
    //         _id: new mongoose.Types.ObjectId(),
    //         brandName: body.name
    //     })
    //     try {
    //         await Brand.findOne({}).exec((err,doc)=>{

    //             brand1.save()  
    //              System.updateOne({'systemName':"安卓"},{$push:{brands:brand1._id}}).exec((err,doc)=>{
    //                 res.status(200).json(doc)
    //             })
    //         })

    //       } catch (error) {
    //        res.status(500).json({
    //          error_code: 1,
    //          message: '服务器错误'
    //        })
    //       }
    // }
}
process.on('unhandledRejection', (reason, promise) => {
    console.log('Unhandled Rejection:', reason)
    // 在这里处理
})

module.exports = systemController